package com.smssalama;

import com.smssalama.i18n.LocalizableFactory;

/**
 * Provides information about current user using SMS Salama
 * 
 * @author Arnold P. Minde
 *
 */
public class SMSSalamaSession {
	private static SMSSalamaSession session = null;

	IUserProfile userSettings;

	private SMSSalamaSession() {
	}

	public synchronized static SMSSalamaSession getSession() {
		if (SMSSalamaSession.session == null) {
			SMSSalamaSession.session = new SMSSalamaSession();
		}
		return SMSSalamaSession.session;
	}

	public synchronized boolean authenticate(String password) {
		this.userSettings = UserProfile.getSettings(password);
		return this.userSettings != null;
	}

	public boolean isAuthenticated() {
		return this.userSettings != null;
	}

	public IUserProfile getUserSettings() {
		return this.userSettings;
	}

	private String locale = null;

	public String getLocale() {
		if(userSettings!=null){
			locale = userSettings.getLocale();
		}
		if(this.locale==null){
			this.locale = System.getProperty("microedition.locale");
		}
		return this.locale;
	}

	public void setLocale(String locale) {
		this.locale = locale;
		if(userSettings!=null){
			userSettings.setLocale(locale);
		}
		LocalizableFactory.clearLocalizableCache();
	}

	public void signOut() {
		this.userSettings = null;
	}
}